package cn.xej.demo;

import cn.xej.demo.service.AnimalService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @Author alicat.xu
 * @Date 2021/10/21 11:34 上午
 */
@Service
public class AnimalManagerFactory implements ApplicationContextAware {

    private static ApplicationContext applicationContext;
    private static Map<String, AnimalService> map = new ConcurrentHashMap<>();

    @PostConstruct
    public void init(){
        map = applicationContext.getBeansOfType(AnimalService.class);
    }

    public void eat(Object param,String food){
        AnimalService animalService = map.get(param);
        animalService.eat(food);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
